package com.sysd.utils;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.Mapper;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.sysd.entity.BaseEntity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @description:
 * @author: bsh
 * @date: 2025/6/3 09:38
 */
public class MyGeneratorUtil {

    public static void main(String[] args) {
        String url = "jdbc:mysql://124.221.255.15:3306/zuiyou-user?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
        String username = "root";
        String password = "123456";
        String author = "bsh";
        String modelNameFather="zuiyou-user\\";
        String parent = "com.sysd";
        String modelName = "";
        String tablePrefix = "sysd_";
        List<String> list = new ArrayList<>();
        list.add("sysd_concern");
        MyGeneratorUtil.generator(list, url, username, password, author, parent, modelName, tablePrefix,modelNameFather);
    }

    /**
     * 生成代码方法
     *
     * @param list 需要生成表集合
     */
    public static void generator(List<String> list, String url, String username, String password, String author, String parent, String modelName, String tablePrefix,String modelNameFather ) {
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author)
                            .outputDir(System.getProperty("user.dir") + "\\"+modelNameFather+"src\\main\\java")
                            .enableSwagger()
                            .disableOpenDir()
                            .commentDate("yyyy-MM-dd")
                            //开启覆盖之前生成的文件
                            .fileOverride();

                })
                .packageConfig(builder -> {
                    builder.parent(parent)
                            .moduleName(modelName)
                            .entity("bean.entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + "\\"+modelNameFather+"src\\main\\resources\\mapper"));
                })
                .strategyConfig(builder -> {
                    Mapper.Builder builder1 = builder.addInclude(list)
                            .addTablePrefix(tablePrefix)
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .superClass(BaseEntity.class)
                            .disableSerialVersionUID()
                            .logicDeleteColumnName("deleted")
                            .addSuperEntityColumns( "create_time", "modify_time")
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .enableHyphenStyle()
                            .mapperBuilder()
                            //.enableBaseResultMap()
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
